package com.nbstar.film.spider.spider;

import com.nbstar.film.spider.http.SipderStringRequest;
import com.nbstar.film.spider.pojo.FilmInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

@Slf4j
public class ImdbSpider extends BaseSpider {
    //private static Logger logger = LoggerFactory.getLogger(ImdbSpider.class);

    public ImdbSpider() {
        this.spiderName = "IMDB";
        setRequest(new SipderStringRequest());
    }

    @Override
    public String getContentId(String filmName)  throws Exception {
        return filmName;
    }

    @Override
    public FilmInfo getFilmInfo(String url) throws Exception {
        FilmInfo result = new FilmInfo();

        if (StringUtils.isEmpty(url)) {
            return result;
        }

        String html = getRequest().getHtml(url);

        Document doc = Jsoup.parse(html);
        Elements ratingValue = doc.getElementsByAttributeValue("itemprop", "ratingValue");
        Elements ratingCount = doc.getElementsByAttributeValue("itemprop", "ratingCount");


        if (ratingValue.size() > 0) {
            result.setAverage(Double.parseDouble(ratingValue.text()));
            log.debug("IMDB评分:" + ratingValue.text());
        }

        if (ratingCount.size() > 0) {
            result.setVotes(Long.parseLong(ratingCount.text().replace(",", "")));
            log.debug("IMDB评论人数:" + ratingCount.text());
        }



        Elements title_wrapper = doc.getElementsByClass("title_wrapper");
        Elements h1 = title_wrapper.select("h1");
        if (h1.size() > 0) {
            if (h1.text().contains("(")) {
                result.setEname(h1.text().substring(0, h1.text().indexOf("(") - 1).trim());
            } else {
                result.setEname(h1.text().trim());
            }
        }

        Elements credit_summary_item = doc.getElementsByClass("credit_summary_item");
        if (credit_summary_item.size() > 0) {
            if (credit_summary_item.get(0).text().contains("Directors:")) {
                result.setDirector(credit_summary_item.get(0).text().replace("Directors:", "").trim());
            } else {
                result.setDirector(credit_summary_item.get(0).text().replace("Director:", "").trim());
            }
            result.setActor(credit_summary_item.get(credit_summary_item.size() -1).text().replace("Stars:", "").replace("| See full cast & crew »", "").trim());
        }

        Elements subtext = doc.getElementsByClass("subtext");

        String[] cats = subtext.get(0).text().split("\\|");
        if (cats.length > 1) {
            result.setCategory(cats[1].trim());
        }

        Elements datetime = subtext.get(0).getElementsByAttribute("datetime");
        if (datetime.size() > 0) {
            String duration = datetime.get(0).text();
            result.setFileDuration(duration);

        }


        Elements title = subtext.get(0).getElementsByAttributeValue("title", "See more release dates");
        if (title.size() > 0) {
            String area = title.get(0).text();

            //String text = subtext.get(0).text().replace("R | ", "");
            //String infos[] = text.split("\\|");

            String[] dateAndAdd = area.split("\\(");

            result.setArriveDate(dateAndAdd[0].trim());
            if (dateAndAdd.length > 1) {
                result.setArriveAddress(dateAndAdd[1].replace(")", ""));
            }
        }


        result.setSuccess(true);
        return result;
    }
}
